package com.example.demo.mapper;

import com.example.demo.dto.CommentDto;
import com.example.demo.dto.DepartmentDto;
import com.example.demo.entity.Department;
import com.example.demo.entity.Student;
import com.example.demo.entity.StudentDep;
import com.example.demo.entity.User;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface StudentMapper {

    @Select("select * from student limit #{page},#{limit}")
    List<Student> select(Integer page,Integer limit);

    //查总数
    @Select("select count(1) from teacher")
    Integer selectStudentCount();

    @Select("select id,user_name,password from student where user_name=#{userName} and password=#{password}")
    Student selectByUserName(String userName,String password);

    @Delete("delete from student where id=#{id}")
    Integer delById(Integer id);

    @Insert("insert into student(name,user_name,password,sex,createdate,my_class,grade)" +
            " values(#{name},#{userName},#{password},#{sex},now(),#{myClass},#{grade})")
    Integer insertStu(Student student);

    //查询我的课程
//    @Select("SELECT t.name as creat ,d.name,type,score,place,time from department d join teacher t on d.create = t.id limit #{page},#{limit}")
    @Select("SELECT a.* from student_dep sd join (SELECT t.id as tid,t.name as creat ,d.id,d.name,type,score,place,time from department d join teacher t on d.createby = t.id limit #{page},#{limit}) a on sd.department_id=a.id where sd.student_id=#{id}")
    public List<DepartmentDto> selectMyDepPage(Integer page,Integer limit,Integer id);
    //查询我的课程总数
    @Select("SELECT count(1) from student_dep sd join (SELECT t.name as creat ,d.id,d.name,type,score,place,time from department d join teacher t on d.createby = t.id) a on sd.department_id=a.id where sd.student_id=#{id}")
    Integer selectMyDepCount(Integer id);

    @Select("select * from student_dep where student_id = #{id}")
    List<StudentDep> selectDepByStuId(Integer id);


    //查询所有可选课程
    @Select("SELECT t.name as creat ,d.id,d.name,type,score,place,time from department d join teacher t on d.createby = t.id limit #{page},#{limit}")
    List<DepartmentDto> selectDepPage(Integer page,Integer limit);

    //查询可选课程总数
    @Select("SELECT count(1) from department d join teacher t on d.createby = t.id")
    Integer selectDepCount();

    //查询是否已经选课
    @Select("select count(1) from student_dep where student_id=#{sId} and department_id=#{depId}")
    Integer selectDepIsSelect(Integer sId,Integer depId);

    //增加选课
    @Insert("insert into student_dep(student_id,department_id) values(#{sId},#{depId})")
    Integer insertDepSelect(Integer sId,Integer depId);

    //清除选课
    @Delete("delete from student_dep where student_id = #{sId} and department_id=#{depId}")
    Integer clearDep(Integer sId,Integer depId);

    //查询我的评价
    @Select("select t.name as teacherName,a.* from teacher t join (select c.*,d.name from comment c join department d on c.department_id = d.id) a on a.teacher_id=t.id where a.student_id=#{sId} limit #{page},#{limit}")
    List<CommentDto> selectMyComment(Integer page,Integer limit,Integer sId);

    //查询我的评价总数
    @Select("select count(1) from comment where student_id=#{sId}")
    Integer selectMyCommentCount(Integer sId);

    //修改教师评价
    @Update("update comment set content = #{content},score =#{score} where id=#{comId}")
    Integer updateComment(String content,Integer score,Integer comId);
}
